#include "Animation.h"

#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/SGD_String.h"

Animation::Animation(void)
{
	//Init
	PlayForward = true;
	isPaused = true;
	FlipImage = false;

	curFrame = 0;
	SpriteSheetID = -1;

	elapsedTime = 0;
}
Animation::~Animation(void)
{
	//Unload if a texture has been correctly loaded.
	//if(SpriteSheetID != -1)
	//CSGD_TextureManager::GetInstance()->UnloadTexture(SpriteSheetID);
}
void Animation::Play(bool loop)
{
	this->curFrame = 0;
	this->elapsedTime = 0;
	this->isPaused = false;
	this->willLoop = loop;
}
void Animation::SetPause(bool paused)
{
	//Set the paused.
	this->isPaused = paused; 
}
RECT Animation::GetCollisionBox(void) const
{
	return this->frames[curFrame].GetCollisionBox();
}
RECT Animation::GetCurrSourceRect(void) const
{
	return this->frames[curFrame].GetSourceRect();
}
void Animation::RestartAnimation(void)
{
	curFrame = 0;
}
void Animation::Update(float deltaTime)
{
	//Only update when the game is not paused.
	if( isPaused == false)
	{
		elapsedTime += deltaTime;
		if( elapsedTime >= frames[curFrame].GetDuration() )
		{
			//Check to see if playing forward
			if( PlayForward == true)
			{
				elapsedTime = 0.0f;
				curFrame++;
				//Check to see if we have ended anim. If so restart.
				if( curFrame >= numFrames && this->willLoop == true )
				{
					curFrame = 0;
				}
				else if( curFrame > numFrames && willLoop == false )
				{
					//TODO: Stop current animation and have the player switch to idle animation. 
				}
			}
			else //Checks to see if playing in reverse
			{
				elapsedTime = 0.0f;
				curFrame--;
				//Check to se if we have reached anim end. If so restart.
				if( curFrame < 0 && willLoop == true )
				{
					curFrame = frames.size() - 1;
				}
				else if( curFrame > numFrames && willLoop == false )
				{
					//TODO: Stop current animation and have the player switch to idle. 
				}
			}
		}
	}
}

void Animation::Render( int posX, int posY) const
{
	//Test 
	if( SpriteSheetID != -1)
	{
		//CSGD_TextureManager::GetInstance()->Draw(SpriteSheetID, 200, 200, 1.0f, 1.0f, &frames[1].GetSourceRect());
		if(this->isPaused == false)
		{
			//Calc width for flip offset
			int DrawWidthDiff = this->GetCollisionBox().right - this->GetCollisionBox().left;

			POINT tempAnch = frames[curFrame].GetAnchor();
			int DrawPosX = frames[curFrame].GetSourceRect().left - tempAnch.x + posX;
			int DrawPosY = frames[curFrame].GetSourceRect().top - tempAnch.y + posY;

			if( FlipImage == false)
			{
				CSGD_TextureManager * pTM = CSGD_TextureManager::GetInstance();
				pTM->Draw(SpriteSheetID, DrawPosX, DrawPosY, 1.0f, 1.0f, &frames[curFrame].GetSourceRect());
			}
			else
			{
				CSGD_TextureManager * pTM = CSGD_TextureManager::GetInstance();
				pTM->Draw(SpriteSheetID, DrawPosX + DrawWidthDiff, DrawPosY, -1.0f, 1.0f, &frames[curFrame].GetSourceRect());
			}
		}
	}
}

//overload for render - crusher
void Animation::Render( int posX, int posY, float xScale, float yScale, DWORD dwColor) const
{
	//Test 
	if( SpriteSheetID != -1)
	{
		//CSGD_TextureManager::GetInstance()->Draw(SpriteSheetID, 200, 200, 1.0f, 1.0f, &frames[1].GetSourceRect());
		if(this->isPaused == false)
		{
			//Calc width for flip offset
			int DrawWidthDiff = this->GetCollisionBox().right - this->GetCollisionBox().left;

			POINT tempAnch = frames[curFrame].GetAnchor();
			int DrawPosX = frames[curFrame].GetSourceRect().left - tempAnch.x + posX;
			int DrawPosY = frames[curFrame].GetSourceRect().top - tempAnch.y + posY;

			if( FlipImage == false)
			{
				CSGD_TextureManager * pTM = CSGD_TextureManager::GetInstance();
				pTM->Draw(SpriteSheetID, DrawPosX, DrawPosY, xScale, yScale, &frames[curFrame].GetSourceRect(),0.0f, 0.0f, 0.0f, dwColor);
			}
			else
			{
				CSGD_TextureManager * pTM = CSGD_TextureManager::GetInstance();
				pTM->Draw(SpriteSheetID, DrawPosX + DrawWidthDiff, DrawPosY, -xScale, yScale, &frames[curFrame].GetSourceRect(),0.0f, 0.0f, 0.0f, dwColor);
			}
		}
	}
}

void Animation::ReverseAnimation(void)
{
	//Toggle.
	PlayForward = !PlayForward;
}
void Animation::SetIgnoredColor( D3DCOLOR col )
{
	D3DXCOLOR temp;
	this->ignoredColor = col;
}
void Animation::SetImagePath( string ImagePath )
{
	//Load in the image to the texture manager
	//this->SpriteSheetID = CSGD_TextureManager::GetInstance()->LoadTexture(ImagePath, ignoredColor);
	this->SpriteSheetID = CSGD_TextureManager::GetInstance()->LoadTexture(ImagePath, ignoredColor);

}
void Animation::SetFrameCount( int count )
{
	this->numFrames = count;
}

void Animation::AddFrame( Frame toAdd )
{
	//Adds a frame to the list
	this->frames.push_back(toAdd);
}

POINT Animation::GetCurrAnchorPoint(void) const
{
	return frames[curFrame].GetAnchor();
}

void Animation::FlipTheImage(bool flip)
{
	FlipImage = flip;
}

float Animation::CurrentAnimationLoopTime( void )
{
	float TotalTime = 0.0f;

	for( int i = 0; i < numFrames; i++ )
	{
		TotalTime += frames[i].GetDuration();
	}

	return TotalTime;

}

POINT Animation::GetShoulderPoint(void) const
{
	return frames[curFrame].GetShoulderPoint();
}

bool Animation::GetIsFlipped(void)
{
	return FlipImage;
}